Ext.define('MelhorCompra.view.RegraDe3', {
    extend: 'Ext.form.Panel',
    xtype: 'regrade3',

    config: {
        title: 'Form',
        cls: 'form',

        items: [{
            xtype: 'fieldset',
            title: 'Regra de 3',
            
            items: [{
                id: 'valA',
                xtype: 'textfield',
                label: 'Se',
                component: {type: 'tel'}
            },{
                id: 'valB',
                xtype: 'textfield',
                label: '=',
                component: {type: 'tel'}
            },{
                id: 'valC',
                xtype: 'textfield',
                label: 'Então',
                component: {type: 'tel'}
            },{
                id: 'valD',
                xtype: 'textfield',
                label: '=',
                readOnly: true,
                cls: 'disabledField'
            }]
        },{
            xtype: 'toolbar',
            docked: 'top',
            layout: {pack: 'center'},
            defaults: {
                iconMask: true
            },
            items: [
                {
                    xtype: 'button',
                    iconCls: 'more',
                    iconMask: true,
                    action: 'exibirMenuLateral'
                },{
                    xtype: 'spacer'
                },{
                    xtype: 'button',
                    text: 'Limpar',
                    iconCls: 'refresh',
                    iconMask: true,
                    handler: function() {
                        this.up('formpanel').reset();
                    }
                },{
                    xtype: 'button',
                    text: 'Calcular',
                    iconCls: 'compose',
                    iconMask: true,
                    ui: 'confirm',
                    handler: function() {
                        calcularRegraDe3();
                    }
                }
            ]
        },{
            xtype: 'toolbar',
            docked: 'bottom',
            layout: {pack: 'center'},
            defaults: {
                iconMask: true
            },
            items: [
                {
                    xtype: 'button',
                    iconCls: 'more',
                    iconMask: true,
                    action: 'exibirMenuLateral'
                },{
                    xtype: 'spacer'
                },{
                    xtype: 'button',
                    text: 'Limpar',
                    iconCls: 'refresh',
                    iconMask: true,
                    handler: function() {
                        this.up('formpanel').reset();
                    }
                },{
                    xtype: 'button',
                    text: 'Calcular',
                    iconCls: 'compose',
                    iconMask: true,
                    ui: 'confirm',
                    handler: function() {
                        calcularRegraDe3();
                    }
                }
            ]
        }],

        listeners: {
            show: function(form, opts){
                Ext.Function.defer(function() {
                   form.down('#valA').focus();
                }, 200);
            }
        }
    }
});


function calcularRegraDe3() {
    var erros = new Array();
    var controller = MelhorCompra.app.getController('MainController');
    var numA = controller.getNumFromField(
        'valA',
        erros,
        'Preencha corretamente o valor do campo "Se"');
    var numB = controller.getNumFromField(
        'valB',
        erros,
        'Preencha corretamente o valor do campo "="');
    var numC = controller.getNumFromField(
        'valC',
        erros,
        'Preencha corretamente o valor do campo "Então"');
    var objD = Ext.getCmp('valD');

    if(erros.length == 0) {
        objD.setValue(numB / numA * numC);
    } else {
        controller.exibirMensagemErro(erros);
    }
}
